/**
 * All rights reserved by www.xandroid.cn
 */
package xfok.net.xandroid.examples;

import java.util.Timer;
import java.util.TimerTask;

import xfok.net.xandroid.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * @author YangHua
 *  转载请注明出处：http://www.xfok.net/android/2010-7-1
 * 2010-7-1 www.xandroid.cn 
 */
public class ImageVibrate  extends Activity{	   
	private ImageView mImageView;
	private Button btn1,btn2;
	private int ScaleTimes=1,ScaleAngle=1;
	private Bitmap bmp;
	private int widthOrig,heightOrig;
	private boolean vibrate=false;
	private Timer timer=new Timer();
	private Matrix matrix=new Matrix();
	private boolean change=true;
	private static int GAP=500;
	private static int angle=5;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_vibrate);
        mImageView=(ImageView)findViewById(R.id.myImageView);
        bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.sample_1);
        widthOrig=bmp.getWidth();
        heightOrig=bmp.getHeight();
        mImageView.setImageBitmap(bmp);
        btn1=(Button)findViewById(R.id.myButton1);
        btn1.setOnClickListener(new OnClickListener(){
	        public void onClick(View v){
	        	vibrate=true; 
	        }
        });
        btn2=(Button)findViewById(R.id.myButton2);
        btn2.setOnClickListener(new OnClickListener(){
	        public void onClick(View v){
	        	vibrate=false;
	        	normal();
	        }
        });
        timer.schedule(task,0,GAP);
    }
    
    private void left(){
        ScaleAngle=-angle;
        int newWidth=widthOrig*ScaleTimes;
        int newHeight=heightOrig*ScaleTimes;
        float scaleWidth=((float)newWidth)/widthOrig;
        float scaleHeight=((float)newHeight)/heightOrig;
        matrix.postScale(scaleWidth, scaleHeight);
        matrix.setRotate(ScaleAngle);
        Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, widthOrig, heightOrig, matrix, true);
        BitmapDrawable myNewBitmapDrawable=new BitmapDrawable(resizeBitmap);
        mImageView.setImageDrawable(myNewBitmapDrawable);
    }
    
    private void right(){
    	ScaleAngle=angle;
        int newWidth=widthOrig*ScaleTimes;
        int newHeight=heightOrig*ScaleTimes;
        float scaleWidth=((float)newWidth)/widthOrig;
        float scaleHeight=((float)newHeight)/heightOrig;
        matrix.postScale(scaleWidth, scaleHeight);
        matrix.setRotate(ScaleAngle);
        Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, widthOrig, heightOrig, matrix, true);
        BitmapDrawable myNewBitmapDrawable=new BitmapDrawable(resizeBitmap);
        mImageView.setImageDrawable(myNewBitmapDrawable);
    }
    
    private void normal(){
        int newWidth=widthOrig*ScaleTimes;
        int newHeight=heightOrig*ScaleTimes;
        float scaleWidth=((float)newWidth)/widthOrig;
        float scaleHeight=((float)newHeight)/heightOrig;     
        matrix.postScale(scaleWidth, scaleHeight);
        matrix.setRotate(0);
        Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, widthOrig, heightOrig, matrix, true);
        BitmapDrawable myNewBitmapDrawable=new BitmapDrawable(resizeBitmap);
        mImageView.setImageDrawable(myNewBitmapDrawable);
    }
    
    Handler handler = new Handler(){  		  
        public void handleMessage(Message msg) {  
            switch (msg.what) {      
            case 1:      
            	if(change){    
            		if(vibrate)left();
            		change = false;    
            	}else{
            		if(vibrate)right();
            		change = true; 
            	}  
                break;      
            }      
            super.handleMessage(msg);  
        }        
    }; 
    
    TimerTask task = new TimerTask(){   
        public void run() {
        	Message message = new Message();      
            message.what = 1;      
            handler.sendMessage(message);   
	    } 
    };
}
